﻿using JHECIS.BLL.Triage.DataServices;
using JHECIS.DataModel.DictModel;
using JHECIS.DataModel.Order;
using JHECIS.DataModel.SysConfig;
using JHECIS.Interface;
using JHECIS.OrderDataService.Entity;
using JHECIS.TriageDataService.Entity;
using JHWpfToolkit.ExeContainer;
using Microsoft.Practices.ServiceLocation;
using MS.Common;
using MS.Common.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;

namespace JHECIS.BLL.Triage.Common
{
    public class DictCacheManager
    {
        /// <summary>
        /// 构造方法 袁凤超 2022-03-09
        /// </summary>
        private DictCacheManager()
        {
            #region 业务接口
            dictServices = ServiceLocator.Current.GetInstance<IDictServices>();
            sysUserService = ServiceLocator.Current.GetInstance<ISysUserService>();
            deptServices = ServiceLocator.Current.GetInstance<IDictDeptService>();
            #endregion

            #region 分诊界面必定使用到的字典初始化
            try
            {
                AdressList = dictServices.GetDicByTableName<adress>();
                SysUserList = sysUserService.GetSysUserInfos()?.ToList();
                CHARGETYPE_list = dictServices.GetDicByTableName<chargetype>();
                COUNTRY_list = dictServices.GetDicByTableName<country>();
                All_GREENROAD_List = dictServices.GetDicByTableName<greenroad>();
                All_TRIAGEMAINCHIEF_list = dictServices.GetDicByTableName<triagemainchief>();
                REGFROM_List = dictServices.GetDicByTableName<regfrom>().OrderBy(c => c.SERIALNO)?.ToList();
                CONCEPTSTATUS_list = dictServices.GetDicByTableName<conceptstatus>().OrderBy(c => c.SERIALNO)?.ToList();
                LEVELCHANGEREASON_List = dictServices.GetDicByTableName<levelchangereason>();
                triageLevelList = dictServices.GetDicByTableName<triagelevel>();
                TriageDirectionList = dictServices.GetDicByTableName<triagedirection>();
                nation_List = dictServices.GetDicByTableName<nation>()?.OrderBy(c => c.ID)?.ToList();
                Occupation_List = dictServices.GetItemDict("IDENTITY");
                marrytype_List = dictServices.GetItemDict("MARITAL_STATUS"); ;
                TriageRegulationList = TriageService.Instance.GetRegulationByModule("分诊");
                WardAreaList = dictServices.GetDicByTableName<wardarea>()?.ToList();
                GreenApplyList = dictServices.GetItemDict("GREENAPPLY");
                ListPRINT_Lable_RULE = dictServices.GetItemDict("PRINT_LABLE_RULE");
                #region 病区等级对照关系
                try
                {
                    if (WardAreaList != null)
                    {
                        for (int i = 0; i < WardAreaList.Count; i++)
                        {
                            string lecels = WardAreaList[i].TRIAGELEVEL;
                            if (lecels != null)
                            {
                                string[] tempLevel = lecels.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                                if (tempLevel != null && tempLevel.Length > 0)
                                {
                                    List<string> levList = tempLevel.ToList();
                                    if (levList.Count > 0)
                                    {
                                        for (int j = 0; j < levList.Count; j++)
                                        {
                                            if (!Level_Area.ContainsKey(levList[j]))
                                            {
                                                Level_Area.Add(levList[j], WardAreaList[i].AREACODE);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.GetLogHelper().Error("病区等级对照出现异常" + ex);
                }
                #endregion
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error("分诊缓存字典加载出现异常" + ex);
            }
            #endregion

            #region 配置开关
            List<JHECIS_ITEM_DICT> TriageRepeatCheckList = dictServices.GetItemDict("TRIAGE_TOP_BUTTONS");
            if (TriageRepeatCheckList != null && TriageRepeatCheckList.Count > 0)
            {
                if (TriageRepeatCheckList[0].CV_VALUE != null)
                {
                    TopButtonStr = TriageRepeatCheckList[0].CV_VALUE;
                }
            }
            List<JHECIS_ITEM_DICT> PATIENT_LIST_TYPE = dictServices.GetItemDict("TRIAGE_PATIENT_LIST_TYPE");
            if (PATIENT_LIST_TYPE != null && PATIENT_LIST_TYPE.Count > 0)
            {
                TriageListType = PATIENT_LIST_TYPE[0].CV_VALUE;
            }
            List<JHECIS_ITEM_DICT> ListAGE_RULE = dictServices.GetItemDict("AGE_CALCUL_RULE");
            if (ListAGE_RULE != null && ListAGE_RULE.Count > 0)
            {
                AGE_CALCUL_RULE = ListAGE_RULE[0].CV_VALUE;
            }
            List<JHECIS_ITEM_DICT> ListTriageSaveUser = dictServices.GetItemDict("TRIAGE_SAVEUSER_RULE");
            if (ListTriageSaveUser != null && ListTriageSaveUser.Count > 0)
            {
                TRIAGE_SAVEUSER_RULE = ListTriageSaveUser[0].CV_VALUE;
            }
            List<JHECIS_ITEM_DICT> NewTriageRuleList = dictServices.GetItemDict("NEW_TRIAGE_RULE");
            if (NewTriageRuleList != null && NewTriageRuleList.Count > 0)
            {
                NEW_TRIAGE_RULE = NewTriageRuleList[0].FN_VALUE;
            }


            #endregion

        }

        /// <summary>
        /// 延迟加载对象 袁凤超  2022-03-08
        /// </summary>
        private static readonly Lazy<DictCacheManager> _lazy =
            new Lazy<DictCacheManager>(() => new DictCacheManager(), LazyThreadSafetyMode.PublicationOnly);

        /// <summary>
        /// 单例对象 袁凤超  2022-03-08
        /// </summary>
        public static DictCacheManager Instance => _lazy.Value;

        /// <summary>
        /// 科室接口
        /// </summary>
        private readonly IDictDeptService deptServices;
        /// <summary>
        /// 用户接口  袁凤超  2022-01-17
        /// </summary>
        private readonly ISysUserService sysUserService;

        /// <summary>
        /// 基础表接口  袁凤超 2022-01-17
        /// </summary>
        private readonly IDictServices dictServices;

        #region 列表数据
        private List<DictDept> _DictDept;
        /// <summary>
        /// 科室列表   袁凤超 2023-06-15
        /// </summary>
        public List<DictDept> DictDept
        {
            get
            {
                if (_DictDept == null)
                {
                    _DictDept = deptServices.GetDictDeptList()?.ToList();
                }
                return _DictDept;
            }
        }
        /// <summary>
        /// 医生和护士列表 袁凤超 2022-03-09
        /// </summary>
        public List<SysUser> SysUserList { get; private set; }

        private List<hospital> _HospitalList;
        /// <summary>
        /// 医院院区列表 袁凤超 2022-03-09
        /// </summary>
        public List<hospital> HospitalList
        {
            get
            {
                if (_HospitalList == null)
                {
                    List<hospital> copyHospital = new List<hospital>();
                    dictServices.GetDicByTableName<hospital>()?.ForEach(c =>
                    {
                        copyHospital.Add(c);
                    });//该方法会缓存数据
                    if (copyHospital.Find(c => c.HOSPITALNAME == "全部") == null)
                    {
                        copyHospital.Add(new hospital() { HOSPITALCODE = "0", HOSPITALNAME = "全部" });
                    }
                    _HospitalList = copyHospital;
                }


                return _HospitalList;
            }
        }

        /// <summary>
        /// 病区列表 袁凤超 2022-03-09
        /// </summary>
        public List<wardarea> WardAreaList { get; private set; }

        #endregion
        #region 分诊界面常用字典
        /// <summary>
        /// 五级地址字典 袁凤超 2022-03-28
        /// </summary>
        public List<adress> AdressList { get; private set; }
        /// <summary>
        /// 付费方式字典  袁凤超 2022-03-28
        /// </summary>
        public List<chargetype> CHARGETYPE_list { get; private set; }
        /// <summary>
        /// 国籍字典  袁凤超 2022-03-28
        /// </summary>
        public List<country> COUNTRY_list { get; private set; }
        /// <summary>
        /// 绿色通道字典  袁凤超 2022-03-28
        /// </summary>
        public List<greenroad> All_GREENROAD_List { get; private set; }
        /// <summary>
        /// 主诉字典 袁凤超 2022-03-28
        /// </summary>
        public List<triagemainchief> All_TRIAGEMAINCHIEF_list { get; private set; }
        /// <summary>
        /// 来院方式字典  袁凤超 2022-03-28
        /// </summary>
        public List<regfrom> REGFROM_List { get; private set; }
        /// <summary>
        /// 神志状态字典  袁凤超 2022-03-28
        /// </summary>
        public List<conceptstatus> CONCEPTSTATUS_list { get; private set; }
        /// <summary>
        /// 等级改变原因字典 袁凤超 2022-03-28
        /// </summary>
        public List<levelchangereason> LEVELCHANGEREASON_List { get; private set; }
        /// <summary>
        /// 分诊等级字典 袁凤超 2022-03-28
        /// </summary>
        public List<triagelevel> triageLevelList { get; private set; }

        /// <summary>
        /// 去向列表字典 袁凤超 2022-03-09
        /// </summary>
        public List<triagedirection> TriageDirectionList { get; private set; }
        /// <summary>
        /// 民族字典 袁凤超 2022-03-09
        /// </summary>
        public List<nation> nation_List { get; private set; }
        /// <summary>
        /// 职业字典 袁凤超 2022-03-09
        /// </summary>
        public List<JHECIS_ITEM_DICT> Occupation_List { get; private set; }
        /// <summary>
        /// 婚姻状态字典 袁凤超 2022-03-09
        /// </summary>
        public List<JHECIS_ITEM_DICT> marrytype_List { get; private set; }
        /// <summary>
        /// 分诊正则赋值规则字典 袁凤超 2022-03-09
        /// </summary>
        public List<Tirage_Regulation> TriageRegulationList { get; private set; }
        /// <summary>
        /// 绿通申请原因字典 袁凤超 2022-03-21
        /// </summary>
        public List<JHECIS_ITEM_DICT> GreenApplyList { get; private set; }
        private List<JHECIS_ITEM_DICT> _NoticeList;
        /// <summary>
        /// 分诊护士喊话字典 袁凤超 2022-03-21
        /// </summary>
        public List<JHECIS_ITEM_DICT> NoticeList
        {
            get
            {
                _NoticeList = dictServices.GetItemDict("TRIAGENOTICE");
                return _NoticeList;
            }
        }

        private List<body> _BodyList;
        /// <summary>
        /// 分诊人体部位字典 袁凤超 2022-03-23
        /// </summary>
        public List<body> BodyList
        {
            get
            {
                if (_BodyList == null)
                {
                    _BodyList = dictServices.GetDicByTableName<body>();
                }
                return _BodyList;
            }
        }

        private List<levelknowlege> _ALL_LevelKonwledgeList;
        /// <summary>
        /// 分诊等级知识库字典 袁凤超 2022-03-23
        /// </summary>
        public List<levelknowlege> ALL_LevelKonwledgeList
        {
            get
            {
                if (_ALL_LevelKonwledgeList == null)
                {
                    _ALL_LevelKonwledgeList = dictServices.GetDicByTableName<levelknowlege>()?.OrderBy(c => c.ORDER_NO)?.ToList();
                }
                return _ALL_LevelKonwledgeList;
            }
        }
        /// <summary>
        /// 病区字典实体 袁凤超  2022-01-20
        /// </summary>
        public Dictionary<string, string> Level_Area = new Dictionary<string, string>();

        private List<groupinjurytype> _GROUPINJURYTYPE_list;
        /// <summary>
        /// 群伤类型字典 袁凤超 2022-03-28
        /// </summary>
        public List<groupinjurytype> GROUPINJURYTYPE_list
        {
            get
            {
                if (_GROUPINJURYTYPE_list == null)
                {
                    _GROUPINJURYTYPE_list = dictServices.GetDicByTableName<groupinjurytype>();
                }
                return _GROUPINJURYTYPE_list;
            }

        }
        public List<JHECIS.BLL.Triage.DataModels.MAINCHIEFTYPE> _MainChiefTypeList;
        /// <summary>
        /// 主诉类型字典 袁凤超 2023-06-07
        /// </summary>
        public List<JHECIS.BLL.Triage.DataModels.MAINCHIEFTYPE> MainChiefTypeList
        {
            get
            {
                if (_MainChiefTypeList == null)
                {
                    _MainChiefTypeList = dictServices.GetDicByTableName<JHECIS.BLL.Triage.DataModels.MAINCHIEFTYPE>();
                }
                return _MainChiefTypeList;
            }

        }
        #endregion
        /// <summary>
        /// 打印选项 袁凤超 2022-04-06
        /// </summary>
        List<JHECIS_ITEM_DICT> ListPRINT_Lable_RULE
        {
            get; set;
        }
        #region 分诊主模块相关配置

        /// <summary>
        /// 分诊患者列表显示模式   袁凤超 2022-03-28
        /// </summary>
        public string TriageListType { get; private set; }
        #endregion

        #region 分诊页面配置项
        /// <summary>
        /// 是否打分诊条 袁凤超  2022-01-20
        /// </summary>
        public bool FZT_Vis
        {
            get
            {
                if (FZTSetting != null)
                {
                    return "1".Equals(FZTSetting.FN_VALUE);
                }
                return true;
            }
        }
        /// <summary>
        /// 分诊条与等级的规则 袁凤超  2022-01-20
        /// </summary>
        public string Fzt_Level
        {
            get
            {
                return FZTSetting?.MEMO ?? string.Empty;
            }
        }
        /// <summary>
        /// 分诊条是否提示 袁凤超  2022-01-20
        /// </summary>
        public string Fzt_Cof
        {
            get
            {
                return FZTSetting?.CODING_SCHM ?? string.Empty;
            }
        }
        /// <summary>
        /// 是否打印腕带 袁凤超  2022-01-20
        /// </summary>
        public bool WD_Vis
        {
            get
            {
                if (WdSetting != null)
                {
                    return "1".Equals(WdSetting.FN_VALUE);
                }
                return true;
            }
        }
        /// <summary>
        /// 腕带与等级的规则 袁凤超  2022-01-20
        /// </summary>
        public string WD_Level
        {
            get
            {
                return WdSetting?.MEMO ?? string.Empty;
            }
        }
        /// <summary>
        /// 腕带是否提示框 袁凤超  2022-01-20
        /// </summary>
        public string WD_Cof
        {
            get
            {
                return WdSetting?.CODING_SCHM ?? string.Empty;
            }
        }
        /// <summary>
        /// 分诊条打印模板名 袁凤超  2022-01-20
        /// </summary>
        public string PRINT_LABLE_FZT
        {
            get
            {
                return FZTSetting?.CV_VALUE ?? "分诊条";
            }
        }
        /// <summary>
        /// 腕带打印模板名 袁凤超  2022-01-20
        /// </summary>
        public string PRINT_LABLE_WD
        {
            get
            {
                return WdSetting?.CV_VALUE ?? "腕带";
            }
        }
        /// <summary>
        /// 是否打印床头卡 袁凤超  2022-01-20
        /// </summary>
        public bool CTK_Vis
        {
            get
            {
                if (CTKSetting != null)
                {
                    return "1".Equals(CTKSetting?.FN_VALUE);
                }
                return true;
            }
        }
        /// <summary>
        /// 床头卡模板名字
        /// </summary>
        public string PRINT_LABLE_CTK
        {
            get
            {
                return CTKSetting?.CV_VALUE ?? "床头卡1";
            }
        }
        /// <summary>
        /// 新建分诊保存规则【1：直接新建 其他：提示】 袁凤超  2022-01-20
        /// </summary>
        public string NEW_TRIAGE_RULE { get; private set; } = string.Empty;
        /// <summary>
        /// 修改分诊保存规则【1为修改分诊不修改操作人，其他：修改分诊修改操作人】 袁凤超  2022-01-20
        /// </summary>
        public string TRIAGE_SAVEUSER_RULE { get; private set; } = string.Empty;
        /// <summary>
        /// 年龄计算规则 袁凤超  2022-01-20
        /// </summary>
        public string AGE_CALCUL_RULE { get; private set; } = string.Empty;
        /// <summary>
        /// 分诊界面右上角附加的按钮显示控制
        /// </summary>
        public string TopButtonStr { get; private set; } = string.Empty;
        private JHECIS_ITEM_DICT _wdSetting;
        /// <summary>
        /// 腕带打印选项 袁凤超 2022-04-06
        /// </summary>
        public JHECIS_ITEM_DICT WdSetting
        {
            get
            {
                if (_wdSetting == null)
                {
                    _wdSetting = ListPRINT_Lable_RULE.FindAll(c => c.DICT_CODE.Equals("WD"))?.Find(c => c.HOSPITAL_NO.Split(new char[] { ',', '，' }).Any(g => g.Equals(UserContext.CurrUser.User.SelectHospitalCode)));

                }
                return _wdSetting;
            }
            set => _wdSetting = value;
        }
        private JHECIS_ITEM_DICT _CTKSetting;
        /// <summary>
        /// 床头卡打印选项 袁凤超 2022-04-06
        /// </summary>
        public JHECIS_ITEM_DICT CTKSetting
        {
            get
            {
                if (_CTKSetting == null)
                {
                    _CTKSetting = ListPRINT_Lable_RULE.FindAll(c => c.DICT_CODE.Equals("CTK"))?.Find(c => c.HOSPITAL_NO.Split(new char[] { ',', '，' }).Any(g => g.Equals(UserContext.CurrUser.User.SelectHospitalCode)));

                }
                return _CTKSetting;
            }
            set => _CTKSetting = value;
        }
        private JHECIS_ITEM_DICT _FZTSetting;
        /// <summary>
        /// 分诊条选项 袁凤超 2022-04-06
        /// </summary>
        public JHECIS_ITEM_DICT FZTSetting
        {
            get
            {
                if (_FZTSetting == null)
                {
                    _FZTSetting = ListPRINT_Lable_RULE.FindAll(c => c.DICT_CODE.Equals("FZT"))?.Find(c => c.HOSPITAL_NO.Split(new char[] { ',', '，' }).Any(g => g.Equals(UserContext.CurrUser.User.SelectHospitalCode)));

                }
                return _FZTSetting;
            }
            set => _FZTSetting = value;
        }
        private string _TriageDeviceNo = string.Empty;
        /// <summary>
        /// 当前分诊台使用的采集设备编号 袁凤超 2022-04-11
        /// </summary>
        public string TriageDeviceNo
        {
            get
            {
                if (string.IsNullOrEmpty(_TriageDeviceNo))
                {
                    try
                    {
                        string ClinetXmlPath = System.AppDomain.CurrentDomain.BaseDirectory + "ClientConfig.xml";
                        _TriageDeviceNo = XmlHelper.GetInstance().Read(ClinetXmlPath, "configuration/" + "TriageDeviceNo", "value");
                    }
                    catch (Exception ex)
                    {
                        LogHelper.GetLogHelper().Error($"读取分诊台设备号失败！{ex}");
                    }
                }
                return _TriageDeviceNo;
            }
        }
        #endregion

        #region 五大中心
        private List<JHECIS_ITEM_DICT> centerDict;
        /// <summary>
        /// 中心字典  袁凤超 2022-03-28
        /// </summary>
        public List<JHECIS_ITEM_DICT> CenterDict
        {
            get
            {
                if (centerDict == null)
                {
                    centerDict = dictServices.GetItemDict("FIVECENTERS")?.OrderBy(o => o.ORDER_NO)?.ToList();
                }
                return centerDict;
            }
        }
        #endregion

        #region 主题相关
        /// <summary>
        /// 是否存在群伤事件主题  袁凤超 2022-03-28
        /// </summary>
        public bool ExistsGroupInjuryTheme
        {
            get
            {
                return File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Themes\\GroupInjury.xaml"));
            }
        }

        /// <summary>
        /// 是否存待诊列表主题  袁凤超 2022-03-28
        /// </summary>
        public bool ExistsToRegistrTheme
        {
            get
            {
                return File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Themes\\ToRegistrList.xaml"));
            }
        }
        #endregion
    }
}
